package com.gdk.ksoft.assembly.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.SystemData;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Mrper on 2014/12/8.
 * 订单日报查询
 */
public class OrderStatementFindDialog extends Dialog {

    public interface OnExcuteQueryListener {
        public void onExcuteQuery(String dateString);
    }

    @ViewInject(R.id.dialog_orderStatementFind_txtDate)
    private TextView txtDate;//查询日期
    @ViewInject(R.id.dialog_orderStatementFind_txtTitle)
    private TextView txtTitle;//标题文本
    @ViewInject(R.id.dialog_orderStatementFind_txtFind)
    private TextView txtFind;//查询按钮
    private OnExcuteQueryListener listener;//数据变化监听器
    private boolean isCanExcuteQuery = true;//是否能够执行查询操作

    public OrderStatementFindDialog(Context context,String titleName,String buttonName, OnExcuteQueryListener listener) {
        super(context, R.style.WindowDialogTheme);
        Window window = getWindow();
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉窗口
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context, R.layout.dialog_order_statement_find, null);
        setContentView(rootView);
        ViewUtils.inject(this, rootView);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.horizontalMargin = 5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        this.listener = listener;
        txtTitle.setText(titleName);
        txtFind.setText(buttonName);
        initArguments();
    }

    /**
     * 初始化数据参数
     */
    public void initArguments(){
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int year = calendar.get(Calendar.YEAR);
        int monthOfYear = calendar.get(Calendar.MONTH);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        String month = (monthOfYear+1<10?"0"+(monthOfYear+1):(monthOfYear+1)+"");
        String day = dayOfMonth<10?"0"+dayOfMonth:""+dayOfMonth;
        txtDate.setText(year+"-"+month+"-"+day);
    }

    @OnClick(value = {R.id.dialog_orderStatementFind_txtDate, R.id.dialog_orderStatementFind_imgClose,
            R.id.dialog_orderStatementFind_txtFind})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.dialog_orderStatementFind_txtDate://日期选择
                final Calendar calendar = Calendar.getInstance(Locale.CHINA);
                new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Calendar slcCalendar = Calendar.getInstance(Locale.CHINA);
                        slcCalendar.set(year,monthOfYear,dayOfMonth);
                        if(com.gdk.ksoft.assembly.utils.system.TimeUtils.computeTimeSpan(calendar, slcCalendar)> SystemData.saleMan.cxddts){
                            Toast.makeText(getContext(),"您只能操作前"+SystemData.saleMan.cxddts+"天的数据!",Toast.LENGTH_SHORT).show();
                            isCanExcuteQuery = false;
                            return;
                        }
                        String month = (monthOfYear+1<10?"0"+(monthOfYear+1):(monthOfYear+1)+"");
                        String day = dayOfMonth<10?"0"+dayOfMonth:""+dayOfMonth;
                        txtDate.setText(year + "-" + month + "-" + day);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH))
                .show();
                break;
            case R.id.dialog_orderStatementFind_imgClose://关闭窗口
                this.dismiss();
                break;
            case R.id.dialog_orderStatementFind_txtFind://查询
                if(!isCanExcuteQuery){
                    Toast.makeText(getContext(),"您只能操作前"+SystemData.saleMan.cxddts+"天的数据!请重新选择查询时间!",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (listener != null) listener.onExcuteQuery(txtDate.getText().toString());
                this.dismiss();
                break;
        }
    }

}
